package com.test;

import java.io.IOException;
import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class Xml {
	
	public static final  String xml = "<xml><ToUserName><![CDATA[ww1cSD21f1e9c0caaa]]></ToUserName><FromUserName><![CDATA[sys]]></FromUserName><CreateTime>1571732272</CreateTime><MsgType><![CDATA[event]]></MsgType><Event><![CDATA[sys_approval_change]]></Event><AgentID>3010040</AgentID><ApprovalInfo><SpNo>201910220003</SpNo><SpName><![CDATA[示例模板]]></SpName><SpStatus>1</SpStatus><TemplateId><![CDATA[3TkaH5KFbrG9heEQWLJjhgpFwmqAFB4dLEnapaB7aaa]]></TemplateId><ApplyTime>1571728713</ApplyTime><Applyer><UserId><![CDATA[WuJunJie]]></UserId><Party><![CDATA[1]]></Party></Applyer><SpRecord><SpStatus>1</SpStatus><ApproverAttr>2</ApproverAttr><Details><Approver><UserId><![CDATA[WangXiaoMing]]></UserId></Approver><Speech><![CDATA[]]></Speech><SpStatus>1</SpStatus><SpTime>0</SpTime></Details><Details><Approver><UserId><![CDATA[XiaoGangHuang]]></UserId></Approver><Speech><![CDATA[]]></Speech><SpStatus>1</SpStatus><SpTime>0</SpTime></Details></SpRecord><SpRecord><SpStatus>1</SpStatus><ApproverAttr>1</ApproverAttr><Details><Approver><UserId><![CDATA[XiaoHongLiu]]>"
			+ "</UserId></Approver><Speech><![CDATA[]]></Speech><SpStatus>1</SpStatus><SpTime>0</SpTime></Details></SpRecord><Notifyer><UserId><![CDATA[ChengLiang]]></UserId></Notifyer><Comments><CommentUserInfo><UserId><![CDATA[LiuZhi]]></UserId></CommentUserInfo><CommentTime>1571732272</CommentTime><CommentContent><![CDATA[这是一个备注]]>"
			+ "</CommentContent><CommentId><![CDATA[6750538708562308220]]></CommentId></Comments><StatuChangeEvent>10</StatuChangeEvent></ApprovalInfo></xml>";
	
	public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder db = dbf.newDocumentBuilder();
		StringReader sr = new StringReader(xml);
		InputSource is = new InputSource(sr);
		Document document = db.parse(is);

		Element root = document.getDocumentElement();
		NodeList nodelist4 = root.getElementsByTagName("ToUserName");
				
		String ToUserName = nodelist4.item(0).getTextContent();
		
		NodeList ApprovalInfo = root.getElementsByTagName("ApprovalInfo");	
		Element ApprovalInfoele = (Element) ApprovalInfo.item(0);
		
		String SpNo = ApprovalInfoele.getElementsByTagName("SpNo").item(0).getTextContent();
		String SpStatus = ApprovalInfoele.getElementsByTagName("SpStatus").item(0).getTextContent();
		String ApplyTime = ApprovalInfoele.getElementsByTagName("ApplyTime").item(0).getTextContent();
		
		
		NodeList Applyer = ApprovalInfoele.getElementsByTagName("Applyer");	
		Element Applyerele = (Element) Applyer.item(0);
		String UserId = Applyerele.getElementsByTagName("UserId").item(0).getTextContent();
		
		System.out.println(SpNo+','+SpStatus+','+ApplyTime+','+UserId);
		//getElementsByTagName("ApprovalInfo")
		System.out.println(document);
		
		//String ApprovalInfostr = ApprovalInfo.item(0);
		//System.out.println(root.getTextContent());
		
		}
	}
